from itsdangerous import TimedJSONWebSignatureSerializer as Serializer,SignatureExpired,BadSignature

from django.conf import settings

def generic_access_token(openid):
    #1 创建序列化器
    serializer = Serializer(settings.SECRET_KEY,3600)

    #2 组织数据
    data = {'openid':openid}
    #3 对数据进行加密
    token = serializer.dumps(data)
    return token.decode()

def check_access_token(token):
    #1创建序列化器
    serializer = Serializer(settings.SECRET_KEY, 3600)
    #2loads 操作  会有异常
    try:
        result = serializer.loads(token)

    except BadSignature:
        return None
    else:
        return result.get('openid')
